package models;

import play.*;
import play.db.DB;
import play.db.jpa.*;

import javax.persistence.*;

import java.sql.SQLException;
import java.util.*;

/**
 * This is the TOpic class which has the attributes topicName, topicCount.
 * There's a list of tutorials "tutorials" which is related to the class
 * Tutorial and lists of Topics "subTopics" and "mergedTopic" which are related
 * to Class Topic Also there's a list of community lists "recommendedTopics"
 * which is related to class CommunityList And a list of registered users
 * "topsFollowers" which is related to class RegisteredUser the methods are:
 * addTutorial, setSubTopic, followTopic and addMergedTopic
 * 
 * 
 * @author Marwa
 */

@Entity
public class Topic extends Model {
	/*** Rana Amr */
	public String topicName;

	/*** Rana Amr */
	@OneToMany
	@JoinColumn(name = "Tuts")
	public List<Tutorial> tutorials;

	/*** Rana Amr */
	@OneToMany
	@JoinColumn(name = "Sub")
	public List<Topic> subTopics;
	/*** Renad Ahmed */
	public int topicCount; 
	
	/***
	 * @author Nourhan Elmalahy
	 */
	@OneToMany
	@JoinColumn(name = "mergedTopics")
	public List<Topic> mergedTopic;
	/***
	 * End Nourhan Elmalahy
	 */
	
	// Start Mayar

	@ManyToMany(mappedBy = "recommendListTopic", cascade = CascadeType.ALL)
	public List<CommunityList> recommendedTopics;

	// End Mayar
	
	
	/**Marwa Elshenawy*/
    @ManyToMany (mappedBy = "followedTops", cascade=CascadeType.ALL)
	public List<RegisteredUser> topsFollowers;
	

	/**
	 * Constructor of the model Topic. Creates new Topics.
	 * 
	 * @author Rana Amr
	 * @param name
	 */
	public Topic(String name) {
		topicName = name;
		this.recommendedTopics = new ArrayList<CommunityList>();
		this.tutorials = new ArrayList<Tutorial>();
		this.subTopics = new ArrayList<Topic>();
		this.mergedTopic = new ArrayList<Topic>();  //Nourhan 
		topsFollowers = new ArrayList<RegisteredUser>();
	}

	/**
	 * Method that takes a tutorial and adds it in the list of tutorials.
	 * 
	 * @author Rana Amr
	 * @param t
	 */
	public void addTutorial(Tutorial t) {
		this.tutorials.add(t);
		this.topicCount++; // renad
		this.save();
	}

	/**
	 * Method that takes another topic and sets it as a sub topic for this
	 * topic.
	 * 
	 * @author Rana Amr
	 * @param t
	 */
	public void setSubTopic(Topic t) {
		this.subTopics.add(t);
		this.save();
	}

	/**
	 * This method takes the uID and tID and return a true if the topic is
	 * not in the list of followed topics, and false if not
	 * 
	 * @param uID
	 *            which is the id of the current user.
	 * @param tID
	 *            which is the id of the topic I'm standing on.
	 * @return boolean true or false
	 * 
	 * @author Marwa
	 */
	public boolean followTopic(long uID, long tID) {
		Topic to = Topic.findById(tID);
		RegisteredUser user = RegisteredUser.findById(uID);
		if (!(user.followedTops.contains(to))) {
			user.followedTops.add(to);
			to.topsFollowers.add(user);
			to.save();
			user.save();
			return true;
		} else {
			return false;
		}
	}
    
    /***
     * This method is for adding the merged topics into the list 
     * @author Nourhan Elmalahy
     * @param t
     */
    public void addMergedTopic(Topic t){
    	this.mergedTopic.add(t);
        this.save();
    }
    /***
     * End Nourhan Elmalahy
     */

}
